package com.pc.scrs.service.impl;

import com.pc.scrs.bean.po.Course;
import com.pc.scrs.bean.vo.CourseVo;
import com.pc.scrs.bean.vo.listVo.CourseListVo;
import com.pc.scrs.bean.vo.pageInfo.CourseListVoPageInfo;
import com.pc.scrs.dao.CourseDao;
import com.pc.scrs.service.ICourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CourseService implements ICourseService {

    @Resource
    private CourseDao courseDao;

    @Override
    public CourseListVoPageInfo courseList(Integer pageNum,Integer pageSize,String title, Integer subjectId, Integer enable, Integer required) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courseList = courseDao.getCourseList(title, subjectId, enable, required);
        List<CourseListVo> courseListVoList = new ArrayList<>();
        PageInfo<Course> coursePageInfo = new PageInfo<>(courseList);
        for(Course course : courseList){
            CourseListVo courseListVo = new CourseListVo();
            courseListVo.setId(course.getId());
            courseListVo.setCover(course.getCover());
            courseListVo.setTitle(course.getTitle());
            courseListVo.setLessonNum(course.getLessonNum());
            courseListVo.setSubjectName(course.getSubject().getTitle());
            courseListVo.setCreateTime(course.getCreateTime());
            courseListVo.setStatus(course.getStatus());
            courseListVo.setEnable(course.getEnable());
            courseListVo.setRequired(course.getRequired());
            courseListVoList.add(courseListVo);
        }
        CourseListVoPageInfo courseListVoPageInfo = new CourseListVoPageInfo();
        courseListVoPageInfo.setCourseListVoList(courseListVoList);
        courseListVoPageInfo.setPageNum(coursePageInfo.getPageNum());
        courseListVoPageInfo.setPageSize(coursePageInfo.getPageSize());
        courseListVoPageInfo.setPages(coursePageInfo.getPages());
        courseListVoPageInfo.setTotal(coursePageInfo.getTotal());
        return courseListVoPageInfo;
    }

    @Override
    public Integer disableCourse(Integer id) {
        Integer result = courseDao.disableCourse(id);
        return result;
    }

    @Override
    public Integer enableCourse(Integer id) {
        Integer result = courseDao.enableCourse(id);
        return result;
    }

    @Override
    public CourseVo queryCourseById(Integer id) {
        Course course = courseDao.queryCourseById(id);
        CourseVo courseVo = new CourseVo();
        courseVo.setId(course.getId());
        courseVo.setCover(course.getCover());
        courseVo.setTitle(course.getTitle());
        courseVo.setLessonNum(course.getLessonNum());
        courseVo.setSubjectId(course.getSubjectId());
        courseVo.setStatus(course.getStatus());
        courseVo.setRequired(course.getRequired());
        courseVo.setDescription(course.getDescription());
        return courseVo;
    }

    @Override
    public Integer updateCourse(CourseVo courseVo) {
        Course course = new Course();
        course.setId(courseVo.getId());
        course.setTitle(courseVo.getTitle());
        course.setCover(courseVo.getCover());
        course.setLessonNum(courseVo.getLessonNum());
        course.setDescription(courseVo.getDescription());
        course.setSubjectId(courseVo.getSubjectId());
        int result = courseDao.updateByPrimaryKeySelective(course);
        return result;
    }
}
